<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<table id="xwzxData"></table> 
  
<script type="text/javascript">
	var editor = null;
	var cpteditor = null;
	var opt;
	var xwzxObj=$('#xwzxData');
	xwzxObj.datagrid({
		url:'article/findarticle.do',
		fitColumns:true,
		striped:true,
		loadMsg:"正在加载中...",
		pagination:true,
		pageNumber:1,
		pageSize:10,
		pageList:[10,20,30,40,50],
		remoteSort:false,
		fit:true,
		columns:[[
				{field:'aids',title:'选择',width:80,align:'center',checkbox:true},   
		        {field:'aid',title:'文章编号',width:100,align:'center',sortable:true}, 
		  		{field:'title',title:'文章标题',width:300,align:'center'},
		  		{field:'addtime',title:'添加时间',width:172,align:'center',editor:'text'},
		  		{field:'tname',title:'类目',width:180,align:'center',editor:'text'},
		  		{field:'author',title:'作者',width:80,align:'center'},
		  		{field:'uname',title:'审核者',width:80,align:'center',editor:'text'},
		  		{field:'_operate',title:'操作',width:200,align:'center',
		        	  formatter:function(value,rowData,index){
		        		  return '<a class="edit" onclick="edit('+rowData.aid+')"></a>&nbsp<a class="preview" onclick="preview('+rowData.aid+')"></a>&nbsp<a class="check" onclick="check('+rowData.aid+')"></a>';
		        	  }
		          }
		 ]],
		 toolbar:[{
				text:"添加",
				iconCls:'icon-add',
				handler:function(){
					add();
				}
			},{
				text:"删除",
				iconCls:'icon-remove',
				handler:function(){
					//获取所有被选中的行
					var rows=xwzxObj.datagrid("getChecked");			
					if(rows.length<=0){//没有选中一行
						$.messager.show({
							title:'温馨提示',
							msg:'请选择您要删除的数据',
							timeout:2000,
							showType:'slide'
						});
					}else{
						$.messager.confirm('信息确认','您确定要删除选中的数据吗？',function(r){
							if(r){													
								var aids = new Array();
								for(var i=0;i<rows.length;i++){
									aids.push(rows[i].aid);
								}
								//将要删除aid发到服务器
								$.post("article/updatearticlesta.do",{aids:aids,status:0},function(data){
									if(data>0){//删除添加
										$.messager.show({
											title:'删除提示',
											msg:'文章信息删除成功',
											timeout:2000,
											showType:'slide'
										});
										$("#xwzxData").datagrid("reload");//页面刷新
									}else{
										$.messager.alert('失败提示','文章类型信息删除失败....','error');
									}
								});
							}
						});
					}
				}
		}],
		  		
		onLoadSuccess:function(data){  //为格式化列添加样式，变成有图标的按钮
			$('.edit').linkbutton({text:'编辑',plain:true,iconCls:'icon-mini-edit'});
			$('.preview').linkbutton({text:'预览',plain:true,iconCls:'icon-search'});
			$('.check').linkbutton({text:'审核',plain:true,iconCls:'icon-ok'});
		}
	}); 
	
	function add(){		
		var dheight = 820;
		var wheight = $(window).height();
		if(wheight<dheight)
		{
			dheight = wheight-10;
		}
		var left = 0;
		var top = 0;
		var bool = false;
		var d=$('<div/>').dialog({
				title : '添加文章',
				href : '${pageContext.request.contextPath}/weixin/back/addartcile.jsp',
				width : 825,
				height : dheight,
				modal : true,
				buttons : [ {
					text : '增加',
					handler : function() {
						if($('#article_artTitle').val()== "" || $('#article_artTitle').val() == undefined || $('#article_artTitle').val() == null)
						{
							$.messager.alert('提示', '请输入标题','warning');
							return;
						}
						if($('#article_artAuth').val()== "" || $('#article_artAuth').val() == undefined || $('#article_artAuth').val() == null)
						{
							$.messager.alert('提示', '请输入作者','warning');
							return;
						}
						if($("#article_artStaTime").datebox('getValue')== "" || $("#article_artStaTime").datebox('getValue') == undefined || $("#article_artStaTime").datebox('getValue') == null)
						{
							$.messager.alert('提示', '请输发布日期','warning');
							return;
						}
						if($('#imageFile').val()== "" || $('#imageFile').val() == undefined || $('#imageFile').val() == null)
						{
							$.messager.alert('提示', '请上传一张图片','warning');
							return;
						}
						if($('#typeid').val()== "" || $('#typeid').val() == undefined || $('#typeid').val() == null)
						{
							$.messager.alert('提示', '请合理选择类型','warning');
							return;
						}
						//获取纯文本内容 
						var text=CKEDITOR.instances.body.document.getBody().getText();
						//替换article_artText的值
						document.getElementById("article_artText").value=text;
						$('#add_ArticleForm').form('submit',{
							url:'article/addarticle.do',
						    	success:function(data){
						    		if(parseInt($.trim(data))==1){//说明是成功的
										$.messager.show({title:'成功提示',msg:'文章信息添加成功',timeout:2000,showType:'slide'});
										d.dialog('close');//关闭面板
										$("#xwzxData").datagrid("reload");//页面刷新									
									}else{
										$.messager.alert("失败提示","文章信息添加失败。。。","error");
									}
						    }
						});
					}
				} ],
				onClose : function() {
					if( editor ){
  						editor.destroy();
  						editor = null;
					}
					if( cpteditor ){
  						cpteditor.destroy();
  						cpteditor = null;
					}
					$(this).dialog('destroy');
				},
				onMove : function(left2,top2)
				{
					if(!bool)
					{
						left = left2;
						top = top2;
						bool = true;
					}
					else
					{
						if(left!=left2||top!=top2)
						{
							$(this).dialog('move',{
								left:left,
								top:top
							}); 
						}
					}
				}
			});
	}
	
	function edit(aid){
		var dheight = 820;
		var wheight = $(window).height();
		if(wheight<dheight)
		{
			dheight = wheight-10;
		}
		var left = 0;
		var top = 0;
		var bool = false;
		 var d=$('<div/>').dialog({
				title : '编辑文章',
				href : '${pageContext.request.contextPath}/weixin/back/updateartcile.jsp',
				width : 825,
				height : dheight,
				modal : true,
				buttons : [ {
					text : '提交',
					handler : function() {
						if($('#article_artTitle').val()== "" || $('#article_artTitle').val() == undefined || $('#article_artTitle').val() == null)
						{
							$.messager.alert('提示', '请输入标题','warning');
							return;
						}
						if($('#article_artAuth').val()== "" || $('#article_artAuth').val() == undefined || $('#article_artAuth').val() == null)
						{
							$.messager.alert('提示', '请输入作者','warning');
							return;
						}
						if($("#article_artStaTime").datebox('getValue')== "" || $("#article_artStaTime").datebox('getValue') == undefined || $("#article_artStaTime").datebox('getValue') == null)
						{
							$.messager.alert('提示', '请输发布日期','warning');
							return;
						}
						if($('#typeid').val()== "" || $('#typeid').val() == undefined || $('#typeid').val() == null)
						{
							$.messager.alert('提示', '请合理选择类型','warning');
							return;
						}
						//获取纯文本内容 
						var text=CKEDITOR.instances.body.document.getBody().getText();
						//替换article_artText的值
						document.getElementById("article_artText").value=text;
						$('#update_ArticleForm').form('submit',{
							url:'article/updateByAid.do',
						    	success:function(data){
						    		if(parseInt($.trim(data))==1){//说明是成功的
										$.messager.show({title:'成功提示',msg:'文章信息添加成功',timeout:2000,showType:'slide'});
										d.dialog('close');//关闭面板
										$("#xwzxData").datagrid("reload");//页面刷新									
									}else{
										$.messager.alert("失败提示","文章信息添加失败。。。","error");
									}
						    }
						});
					}
				} ],
				onLoad : function() {
					//异步请求，取到所有的文章类型		
					$.post("article/findarticleByAid.do",{aid:aid}, function(data){
						$("#article_artAid").val(data.aid);
						$("#article_artTitle").val(data.title);
						$("#article_artAuth").val(data.author);
						$("#article_artStaTime").datebox('setValue',data.addtime);	
						$("#typeid").append("<option value='"+ data.typeid +"'>" + data.tname+ "</option>");
						$("#article_artWeight").val(data.weight);
						CKEDITOR.instances.body.setData(data.body);
						var str="";
						var pics=data.picurl;
						str+="<img src='"+pics+"' width='100px'>&nbsp;";
						$("#article_artPic_show").html($(str));
					},"json");
				},
				onClose : function() {
					 if( editor ){
  						editor.destroy();
  						editor = null;
					}
					if( cpteditor ){
  						cpteditor.destroy();
  						cpteditor = null;
					}
					$(this).dialog('destroy'); 
				}
			}); 
	}
	   
	function preview(aid){
		alert("预览"+aid)
	}
	function check(aid){
		//将aid发到服务器
		$.post("article/checkarticle.do",{aid:aid},function(data){
			if(data>0){//删除添加
				$.messager.show({
					title:'删除提示',
					msg:'文章审核成功',
					timeout:2000,
					showType:'slide'
				});
				$("#xwzxData").datagrid("reload");//页面刷新
			}else{
				$.messager.alert('失败提示','文章审核失败....','error');
			}
		});
	}
</script> 
